package com.example.cinema.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.database.DataBaseMethod;
import com.example.cinema.databinding.ActivityCartpayBinding;
import com.example.cinema.request.RequestSend;

import java.util.Map;

//卡密充值
public class ActivityCartPay extends AppCompatActivity {

    private ActivityCartpayBinding ACB;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ACB = DataBindingUtil.setContentView(this, R.layout.activity_cartpay);
        init();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }

    private UserAttribute attribute;
    private void init() {
        attribute = (UserAttribute) getApplication();
        ACB.btnclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = ACB.crattext.getText().toString();
                if(text.isEmpty())
                {
                    Toast.makeText(getApplicationContext(),"请输入卡密",Toast.LENGTH_SHORT).show();
                    return;
                }
                ThreadReq(text);
            }
        });
        ACB.playbtns.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("QQQ",attribute.getPlayUrl());
                Uri uri = Uri.parse(attribute.getPlayUrl());
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });
        ACB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void ThreadReq(String text)
    {
        new Thread()
        {
            @Override
            public void run() {
                RequestSend requestSend = new RequestSend(getApplicationContext());
                boolean bb = requestSend.CartChong(text,attribute);
                Message message = new Message();
                if(bb)
                {
                    message.what = 1000;
                }else {
                    message.what = 1001;
                }
                handler.sendMessage(message);
            }
        }.start();
    }

    private void ThreadReqs()
    {
        new Thread()
        {
            @Override
            public void run() {

                TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                String id = manager.getDeviceId();
                DataBaseMethod baseMethod = new DataBaseMethod(getApplicationContext(),"userinfo");
                Map<String,Object> map = baseMethod.GetUserData();
                Log.i("KKJ",map.get("nam").toString());
                Log.i("KKJ",map.get("psw").toString());
                Log.i("KKJ",map.get("uid").toString());

                new RequestSend(getApplicationContext()).UserLong(map.get("nam").toString(),map.get("psw").toString(),attribute,id);
            }
        }.start();
    }

    Handler handler = new Handler(Looper.getMainLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1000)
            {
                Toast.makeText(getApplicationContext(),"充值成功",Toast.LENGTH_LONG).show();
                ThreadReqs();
                return;
            }
            if(msg.what==1001)
            {
                Toast.makeText(getApplicationContext(),"充值失败卡密错误",Toast.LENGTH_LONG).show();
                return;
            }
        }
    };
}
